/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.border.BevelBorder;
import org.aswing.BorderLayout;
import org.aswing.colorchooser.JColorMixer;
import org.aswing.colorchooser.JColorSwatches;
import org.aswing.colorchooser.PreviewColorIcon;
import org.aswing.Insets;
import org.aswing.JLabel;
import org.aswing.JPanel;
import org.aswing.plaf.basic.BasicColorChooserUI;
import org.aswing.SoftBox;
import org.aswing.SoftBoxLayout;

import fever.app.local.Localisation;
import fever.app.local.LocalisationEvent;
import fever.app.local.LocalisationListener;

import fvaswing.components.colorchooser.FvColorChooserResources;
import fvaswing.components.colorchooser.FvColorMixerUI;
import fvaswing.components.colorchooser.FvColorSwatchesUI;
import fvaswing.components.FvColorChooser;

/**
 * UI implementation for {@link fvaswing.components.FvColorChooser} 
 * component.
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.colorchooser.FvColorChooserUI extends BasicColorChooserUI
	implements LocalisationListener
{
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private static var _instance : FvColorChooserUI;
	
	private var chooser : FvColorChooser;
	private var _oldLabel :JLabel;
	private var _currentLabel : JLabel;	private var _rightPane : JPanel;
	
	 
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link FvColorChooserUI} instance.
	 */
	public static function getInstance() : FvColorChooserUI
	{
		if( !_instance ) _instance = new FvColorChooserUI();
		return _instance; 	
	}
	
	/**
	 * Triggered when Localisation language change.
	 */
	public function onLocalisationUpdate( event : LocalisationEvent ) : Void
	{
		chooser.getTabbedPane().setTitleAt( 0, FvColorChooserResources.getInstance().swatchTitle );
    	chooser.getTabbedPane().setTitleAt( 1, FvColorChooserResources.getInstance().mixerTitle );
    	
    	_oldLabel.setText( FvColorChooserResources.getInstance().oldLabel );
		_currentLabel.setText( FvColorChooserResources.getInstance().currentLabel );	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvColorChooserUI()
	{
		super();
		
		Localisation.addLocalisationListener( this );
	}
	
	private function layoutComponents() : Void
	{
		chooser.setLayout( new BorderLayout( 5, 5 ) );
		chooser.append( chooser.getTabbedPane(), BorderLayout.CENTER );
		chooser.append( _getRightPanel(), BorderLayout.EAST );
		chooser.getCancelButton().setMargin( new Insets( 0, 5, 0, 5 ) );
	}
	
	private function _getRightPanel() : JPanel
	{
		if( !_rightPane )
		{
			_rightPane = new JPanel( new BorderLayout( 5, 5 ) );
			_rightPane.addEventListener( JPanel.ON_CREATED, _onRightPanelAvailable, this );
		}
		return _rightPane;
	}
	
	private function _onRightPanelAvailable( source : JPanel ) : Void
	{
		var bTopPane : SoftBox = SoftBox.createVerticalBox( 5, SoftBoxLayout.TOP );
		var bCenterPane : SoftBox = SoftBox.createVerticalBox( 5, SoftBoxLayout.CENTER );
		var bBottomPane : SoftBox = SoftBox.createVerticalBox( 5, SoftBoxLayout.BOTTOM );
		
		bTopPane.append( chooser.getOkButton() );
		bTopPane.append( chooser.getCancelButton() );
		
		bCenterPane.append( _getOldLabel() );
		bCenterPane.append( _getPreviewLabel() );
		bCenterPane.append( _getCurrentLabel() );
		
		bBottomPane.append( chooser.getPickerButton()  );
		
		_rightPane.append( bTopPane, BorderLayout.NORTH );
		_rightPane.append( bCenterPane, BorderLayout.CENTER );
		_rightPane.append( bBottomPane, BorderLayout.SOUTH );
	}
	
	/** overrides to control myself */
	private function createPreviewColorIcon() : Void {}
	
	/** overrides to control myself */
	private function createPreviewColorLabel() : Void {}
	
	private function _getOldLabel() : JLabel
	{
		if( !_oldLabel )
			_oldLabel = new JLabel( FvColorChooserResources.getInstance().oldLabel );
		
		return _oldLabel;
	}
	
	private function _getCurrentLabel() : JLabel
	{
		if( !_currentLabel )
			_currentLabel = new JLabel( FvColorChooserResources.getInstance().currentLabel );
		
		return _currentLabel;
	}
	
	private function addChooserPanels():Void
	{
    	var sw : JColorSwatches = new JColorSwatches();
    	sw.setUI( new FvColorSwatchesUI() );
    	
    	var mixer : JColorMixer = new JColorMixer();
    	mixer.setUI( new FvColorMixerUI() );
    	
    	chooser.addChooserPanel( FvColorChooserResources.getInstance().swatchTitle, sw );
    	chooser.addChooserPanel( FvColorChooserResources.getInstance().mixerTitle, mixer );
    }
    
    private function _getPreviewLabel() : JLabel
    {
    	if( !previewColorLabel )
    	{
    		previewColorLabel = new JLabel( previewColorIcon );
			
			var bb : BevelBorder = new BevelBorder( null, BevelBorder.LOWERED );
			bb.setThickness( 1 );
			previewColorLabel.setBorder( bb );	
			previewColorLabel.setIcon( _getPreviewColorIcon() );
    	}
    	
    	return previewColorLabel;
    }
    
    private function _getPreviewColorIcon() : PreviewColorIcon
    {
    	if( !previewColorIcon )
    	{
    		previewColorIcon = new PreviewColorIcon( 60, 60, PreviewColorIcon.VERTICAL );	
    	}	
    	return previewColorIcon;
    }
    
    /** override */
    private function __selectedColorChanged() : Void
    {
		_getPreviewColorIcon().setCurrentColor( chooser.getSelectedColor() );
		
		_getPreviewLabel().repaint();
	}
}